package com.sykj.wangkun.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sykj.exception.BizException;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import com.sykj.wangkun.bean.AdmUser;
import com.sykj.wangkun.mapper.SysUserMapper;
import com.sykj.wangkun.service.SysUserService;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.jsonwebtoken.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: AdmUserServiceImpl
 * @Package: com.sykj.wangkun.service.Impl
 * @program: BeautyClub
 * @description: 后台管理员实现接口
 * @author: Mr.Wang
 * @create: 2020-07-20 09:12
 **/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, AdmUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 功能描述:查询后台管理员的信息
     * @author 姓名
     * @date
     */
    @Override
    public List<AdmUser> queryall() {
        List<AdmUser> queryall = sysUserMapper.queryall();
        return queryall;
    }
    /**
     * 功能描述:添加管理员信息
     * @author 姓名
     * @date
     */
    @Override
    public ResultBody addsysuser(AdmUser admUser) {

        AdmUser admUser1 = sysUserMapper.queryByName(admUser.getAccount());
        if ("".equals(admUser1)||admUser1==null){
            Map map=new HashMap();
            map.put("password",admUser.getAccount());
            //生成随机数
            String code= RandomStringUtils.randomNumeric(8);
            String mima=Jwts.builder().setClaims(map).setIssuedAt(new Date()).signWith(SignatureAlgorithm.HS256,code).compact();
            System.out.println("加密的数字是："+mima);
            admUser.setSalt(code);
            admUser.setPassword(mima);
            sysUserMapper.addsysuser(admUser);
            return new ResultBody(CommonEnum.SUCCESS);

        }

        return new ResultBody(CommonEnum.SYS_USER_EXIS);

    }
    /**
     * 功能描述:删除管理员信息 假删除
     * @author 姓名
     * @date
     */
    @Override
    public ResultBody updateByisDel(Integer userId) {
        sysUserMapper.updateByisDel(userId);
        return new ResultBody(CommonEnum.SUCCESS);
    }


    /**
     * 功能描述:根据输入的账号进行登录 查询
     * @author 姓名
     * @date
     */
    @Override
    public ResultBody login(String account, String password) {
//        查询到账号信息
        AdmUser admUser = sysUserMapper.queryByAccount(account);
        if (admUser==null){
            return new ResultBody(CommonEnum.NOT_FOUND);
        }
        //得到加密的盐值
        String salt = admUser.getSalt();
        System.out.println("盐是："+salt);
        //得到加密的密码
        String password1 = admUser.getPassword();
        System.out.println("加密密码是："+password1);

        Map   map1 = Jwts.parser().setSigningKey(salt).parseClaimsJws(password1).getBody();
        System.out.println("------------"+map1);

        System.out.println("解密后的密码是："+map1.get("password"));

        String password2=(String) map1.get("password");

        System.out.println("===================="+password2);


        if(!password2.equals(password)){
            return new ResultBody(CommonEnum.SYS_USER_LONGE_NO);
        }
        return new ResultBody(CommonEnum.SUCCESS);

        //throw new BizException(CommonEnum.SYS_USER_LONGE_NO);

    }
    /**
     * 功能描述:根据输入的账号进行查询
     * @author 姓名
     * @date
     */
    @Override
    public ResultBody queryByAccount(String account) {

        AdmUser admUser = sysUserMapper.queryByAccount(account);
        if (admUser==null){
            return new ResultBody(CommonEnum.NOT_FOUND);
        }

        return new ResultBody(CommonEnum.SUCCESS,admUser);
    }

}
